//   Copyright 2012,2013 Vaughn Vernon
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.

package com.saasovation.agilepm.port.adapter.messaging;

/**
 * 产品讨论专属所有者ID
 * @author learn
 */
public class ProductDiscussionExclusiveOwnerId {

    /**
     * 前缀：agilepm.product.discussion.
     */
    private static final String PREFIX = "agilepm.product.discussion.";
    /**
     * 身份识别ID
     */
    private String id;

    /**
     * 来自编码ID
     * @param anEncodedId       编码ID
     * @return                  产品讨论专属所有者ID
     */
    public static ProductDiscussionExclusiveOwnerId fromEncodedId(
            String anEncodedId) {

        // 先定义一个无效的产品讨论专属所有者ID
        ProductDiscussionExclusiveOwnerId id = null;

        if (ProductDiscussionExclusiveOwnerId.isValid(anEncodedId)) {
            id = new ProductDiscussionExclusiveOwnerId(
                    anEncodedId.substring(PREFIX.length()));
        }

        return id;
    }

    /**
     * 是否验证过
     * @param anEncodedId       编码ID
     * @return                  返回布尔
     */
    public static boolean isValid(
            String anEncodedId) {
        // 如果编码ID起始于前缀 且 编码ID的长度 大于 前缀长度，返回验证过；否则返回验证未过
        return anEncodedId.startsWith(PREFIX) &&
                anEncodedId.length() > PREFIX.length();
    }

    public ProductDiscussionExclusiveOwnerId(String anId) {
        super();

        this.id = anId;
    }

    /**
     * 获取编码过的值
     * @return  返回字符串
     */
    public String encoded() {
        return PREFIX + this.id;
    }

    /**
     * 获取ID
     * @return  返回字符串
     */
    public String id() {
        return this.id;
    }
}
